/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl.extend;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.context.ContextThead;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.system.application.extend.AuthSettingApplication;
import com.foreveross.bsl.system.application.extend.vo.AuthSettingVO;
import com.foreveross.bsl.system.model.domain.AuthSetting;


/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-6-7
 *
 */
@Named("authSettingApplication")
public class AuthSettingApplicationImpl implements AuthSettingApplication {
	
	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AuthSettingVO saveEntity(AuthSettingVO entity) {
		AuthSetting authSetting = new AuthSetting();
		BeanHelper.copyProperties(authSetting, entity);
		authSetting.setCreateDate(new Date());
		authSetting.setUsername(ContextThead.getThreadVar());
		authSetting.save();
		entity.setId(authSetting.getId());
		
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AuthSettingVO updateEntity(String id, AuthSettingVO entity) {
		AuthSetting authSetting = new AuthSetting();
//		authSetting = (AuthSetting)authSetting.findOne(id);
		BeanHelper.copyProperties(authSetting, entity);
		authSetting.save();
		BeanHelper.copyProperties(entity, authSetting);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		AuthSetting authSetting = new AuthSetting();
		authSetting = (AuthSetting)authSetting.findOne(id);
		authSetting.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public AuthSettingVO getEntity(String id) {
		AuthSetting authSetting = new AuthSetting();
		authSetting = (AuthSetting)authSetting.findOne(id);
		AuthSettingVO vo = new AuthSettingVO();
		BeanHelper.copyProperties(vo, authSetting);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public List<AuthSettingVO> getEntityByUserName(String username) {
		AuthSetting authSetting = new AuthSetting();
		OperatEnum operat = OperatEnum.EQ;
		String proName = "username";
		List<AuthSetting> authSettings = (List<AuthSetting>)authSetting.findListByProperty(proName, operat,username);
		if(authSettings != null){
			List<AuthSettingVO> result = new ArrayList<AuthSettingVO>();
			AuthSettingVO vo;
			for (Iterator iterator = authSettings.iterator(); iterator.hasNext();) {
				AuthSetting obj = (AuthSetting) iterator.next();

				vo = new AuthSettingVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<AuthSettingVO> findAll() {
		AuthSetting authSetting = new AuthSetting();
		Iterable iterable = authSetting.findAll();
		if(iterable != null){
			List<AuthSettingVO> result = new ArrayList<AuthSettingVO>();
			AuthSettingVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				AuthSetting obj = (AuthSetting) iterator.next();

				vo = new AuthSettingVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<AuthSettingVO> findByPageable(Pageable pageable) {
		AuthSetting authSetting = new AuthSetting();
		Page page = authSetting.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<AuthSettingVO> result = new ArrayList<AuthSettingVO>();
			AuthSettingVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				AuthSetting obj = (AuthSetting) iterator.next();

				vo = new AuthSettingVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<AuthSettingVO> pageVO = new PageImpl<AuthSettingVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

}
